home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / FILCOUNT.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  3KB  |  99 lines

  1. /*
  2. **  FILCOUNT.C - counts directories and /or files in a directory
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #undef TRUE
  11. #undef FALSE
  12. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  13.  
  14. #ifdef __ZTC__
  15.  #if __ZTC__ < 0x210    /* ZTC 2.0 and lower...                         */
  16.   #include <mflfiles.h> /*  ...don't do recursive find first/next...    */
  17.   #include <msport.h>   /*   ...so use MFLZT library functions          */
  18.  #else                  /* ZTC 2.1 has MSC look-alike functions         */
  19.   #include <dos.h>
  20.  #endif
  21. #elif defined(__TURBOC__)
  22.  #include <dir.h>
  23.  #include <dos.h>
  24.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  25.  #define _dos_findnext(b) findnext(b)
  26.  #define find_t ffblk
  27.  #define _A_SUBDIR FA_DIREC
  28.  #define attrib ff_attrib
  29.  #define name ff_name
  30. #else                   /* assume MSC/QC                                */
  31.  #include <dos.h>
  32.  #include <errno.h>
  33. #endif
  34.  
  35. #undef SUCCESS
  36. #define SUCCESS 0
  37.  
  38. #define LAST_CHAR(str) (str)[strlen(str) - 1]
  39.  
  40. unsigned DirCount = 0, FileCount = 0;
  41.  
  42. /*
  43. **  Arguments: 1 - directory to search
  44. **             2 - search subdirectories: TRUE or FALSE
  45. */
  46.  
  47. void do_dir(char *path, int recurse_flag)
  48. {
  49.       char search[67], new[67];
  50.       struct find_t ff;
  51.  
  52.       strcpy(search, path);
  53.       if ('\\' != LAST_CHAR(search))
  54.             strcat(search, "\\");
  55.       strcat(search, "*.*");
  56.       if (SUCCESS == _dos_findfirst(search, 0xff, &ff)) do
  57.       {
  58.             if ('.' == *ff.name)
  59.                   continue;
  60.             if (ff.attrib & _A_SUBDIR)
  61.             {
  62.                   DirCount++;
  63.                   if (recurse_flag)
  64.                   {
  65.                         strcpy(new, path);
  66.                         if  ('\\' != LAST_CHAR(new))
  67.                               strcat(new, "\\");
  68.                         strcat(new, ff.name);
  69.                         do_dir(new, recurse_flag);
  70.                   }
  71.             }
  72.             else  FileCount++;
  73.       } while (SUCCESS == _dos_findnext(&ff));
  74. }
  75.  
  76. /*
  77. **  Simple resursive file/directory counter
  78. **
  79. **  Usage: FILCOUNT [path_name] [{Y | N}]
  80. **
  81. **  Notes: 1. If a path name isn't specified, the current directory is assumed
  82. **         2. Default recursion flag is FALSE
  83. **         3. Path must be specified in order to specify the recursion flag
  84. */
  85.  
  86. void main(int argc, char *argv[])
  87. {
  88.       char *Dir =".";
  89.       LOGICAL recurse = FALSE;
  90.  
  91.       if (1 < argc)
  92.             Dir = argv[1];
  93.       if (2 < argc)
  94.             recurse = (NULL != strchr("Yy", *argv[2]));
  95.       do_dir(Dir, recurse);
  96.       printf("Counted: %d Directories and %d Files\n",
  97.               DirCount, FileCount);
  98. }
  99.